<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局头部 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部结束 -->
        <!-- 主体结构 -->
        <el-tree
          :data="data"
          icon-class=" "
        >
          <template slot-scope="{data,node}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <i v-if="data.children.length" :class="node.expanded?'el-icon-remove-outline':'el-icon-circle-plus-outline'" />
                <i v-else class="el-icon-star-on" />
                <span>{{ data.name }}</span>
              </el-col>

              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 主体结构结束 -->
      </el-card>
      <!-- 添加或者编辑功能的弹层子组件 -->
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="50%"
      >
        <AddorEdit v-if="dialogVisible" :id="curId" :is-edit="isEdit" @close="hClose" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      data: [],
      title: '编辑部门',
      dialogVisible: false,
      curId: '',
      isEdit: false
    }
  },
  created() {
    this.getDepartmentsData()
  },
  methods: {
    // 获取组织架构数据
    async getDepartmentsData() {
      const res = await getDepartments()
      console.log(res)
      res.data.depts.shift()
      // 调用封装好的函数 将平铺的数组数据转为树形结构
      this.data = tranListToTreeData(res.data.depts)
    },
    hAdd(id) {
      // 弹层显示
      this.dialogVisible = true
      this.title = '新增部门'
      this.curId = id
    },
    hClose() {
      this.dialogVisible = false
      // 重新请求数据渲染页面
      this.getDepartmentsData()
    },
    // 编辑功能
    hEdit(id) {
      // 弹层显示
      this.dialogVisible = true
      this.curId = id
      this.isEdit = true
    },
    // 删除功能
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        console.log(res)
        this.hClose()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
